load("@rules_rust//rust:defs.bzl", "rust_doc_test", "rust_library", "rust_test", "rust_test_suite")
load("//bazel:defs.bzl", "rust_bench")
load("//bazel:fuzz_testing.bzl", "DEFAULT_RUSTC_FLAGS_FOR_FUZZING")

package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
    # Keep sorted.
    "//rs/crypto/internal/crypto_lib/types",
    "//rs/crypto/sha2",
    "//rs/protobuf",
    "@crate_index//:serde",
    "@crate_index//:serde_bytes",
    "@crate_index//:thiserror",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//rs/crypto/test_utils/reproducible_rng",
    "//rs/crypto/tree_hash/test_utils",
    "@crate_index//:assert_matches",
    "@crate_index//:criterion",
    "@crate_index//:maplit",
    "@crate_index//:proptest",
    "@crate_index//:prost",
    "@crate_index//:rand",
    "@crate_index//:serde_cbor",
]

MACRO_DEV_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:test-strategy",
]

rust_library(
    name = "tree_hash",
    srcs = glob(["src/**"]),
    crate_name = "ic_crypto_tree_hash",
    rustc_flags = select({
        "//bazel:fuzzing_code_enabled": DEFAULT_RUSTC_FLAGS_FOR_FUZZING,
        "//conditions:default": [],
    }),
    version = "0.9.0",
    deps = DEPENDENCIES,
)

rust_test(
    name = "tree_hash_test",
    crate = ":tree_hash",
    deps = DEV_DEPENDENCIES,
)

rust_test_suite(
    name = "tree_hash_integration",
    srcs = glob(["tests/**"]),
    proc_macro_deps = MACRO_DEV_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [":tree_hash"],
)

rust_doc_test(
    name = "tree_hash_doc_test",
    crate = ":tree_hash",
    deps = DEV_DEPENDENCIES + ["@crate_index//:rand_chacha"],
)

rust_bench(
    name = "tree_hash_bench",
    testonly = True,
    srcs = ["benches/tree_hash.rs"],
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [":tree_hash"],
)

rust_bench(
    name = "flat_map_bench",
    testonly = True,
    srcs = ["benches/flat_map.rs"],
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [":tree_hash"],
)
